#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sync_baidu.sync_baidu
from util.logger import process_log
from sync_baidu.sync_baidu import SyncException
__author__ = 'kongchao'

START_ERROR = "start_error.lock"
END_ERROR = "end_error.lock"

def monitor_error():
    init_monitor()
    write_file(START_ERROR)
    try:
        sync_error = sync_baidu.sync_baidu.SyncError()
        sync_error.sync_error_call()
    except SyncException, se:
        process_log().error(se.message)
    except Exception, e:
        process_log().error(u"同步error数据失败,%s" % e)
    finally:
        write_file(END_ERROR)


def write_file(lock_file):
    try:
        lock_file = os.path.join(os.getcwd(), 'conf', lock_file)
        with open(lock_file, 'w') as f:
            pass
    except Exception, e:
        process_log().warning(u"写入文件失败,%s" % e)

def init_monitor():
    monitor_list = (START_ERROR, END_ERROR)
    try:
        for data_file in monitor_list:
            data_file = os.path.join(os.getcwd(),'conf', data_file)
            if os.path.exists(data_file):
                os.remove(data_file)
    except Exception, e:
        process_log().warning(u"初始化监控环境失败,%s" % e)

if __name__ == "__main__":
    monitor_error()





